package com.exam.service.impl;

import com.exam.mapper.*;
import com.exam.po.ExamManage;
import com.exam.po.Paper;
import com.exam.service.ExamService;
import com.exam.service.QuestionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author mahaoqi
 * @ClassName ExamServiceImpl
 * @Description 添加考试+自动组卷的业务功能设计
 * @Date 2021/1/12 11:00
 * @Version 1.0
 **/
@Service
public class ExamServiceImpl implements ExamService {

    @Resource
    private PaperMapper paperMapper;
    @Resource
    private ExamManageMapper examManageMapper;
    @Resource
    private QuestionService questionService;



    /*
     * @Author mahaoqi
     * @Description 添加考试
     * @Date 2021/1/13 10:34
     * @Param [paper, examManage, teacherid]
     * @return boolean
     **/
    @Override
    public boolean addPaper(Paper paper,ExamManage examManage,Integer teacherid) {
         paperMapper.insertOne(paper);
         Integer paperid = paper.getPaperid();
         String subject = paper.getSubject();
         Integer fillNum = paper.getFillNum();
         Integer judgeNum = paper.getJudgeNum();
         Integer multiNum = paper.getMultiNum();

         if(paperid!=null && subject!=null){
             Integer[] fillIds = questionService.findFillIds(subject,fillNum);
             Integer[] judgeIds = questionService.findJudgeIds(subject,judgeNum);
             Integer[] multiIds = questionService.findMultiIds(subject,multiNum);

             examManage.setPaperid(paperid);
             examManage.setTcid(examManageMapper.findTCid(teacherid,subject));
             if(examManageMapper.insertSelective(examManage)>0){
                 return questionService.addPaperManage(paperid,fillIds,judgeIds,multiIds);
             }


         }
        return false;
    }

    /*
     * @Author mahaoqi
     * @Description 查找所有状态为 已审核的考试
     * @Date 2021/1/13 10:35
     * @Param [teacherid, page, pageSize]
     * @return java.util.List<com.exam.po.ExamManage>
     **/
    @Override
    public List<ExamManage> findList(Integer teacherid, int page, int pageSize) {
        return examManageMapper.findList(teacherid,(page-1)*pageSize,pageSize);
    }

    @Override
    public Integer findListCount(Integer teacherid) {
        return examManageMapper.findListCount(teacherid);
    }

    /*
     * @Author mahaoqi
     * @Description 查找所有状态为 未通过的考试
     * @Date 2021/1/13 10:35
     * @Param [teacherid, page, pageSize]
     * @return java.util.List<com.exam.po.ExamManage>
     **/
    @Override
    public List<ExamManage> findNoList(Integer teacherid, int page, int pageSize) {
        return examManageMapper.findNoList(teacherid,(page-1)*pageSize,pageSize);
    }

    /*
     * @Author mahaoqi
     * @Description 查找添加考试是需要用到的  tcid
     * @Date 2021/1/13 10:36
     * @Param [teacherid, coursename]
     * @return java.lang.Integer
     **/
    @Override
    public Integer findTCid(Integer teacherid, String coursename) {
        return examManageMapper.findTCid(teacherid,coursename);
    }

    /*
     * @Author mahaoqi
     * @Description 修改状态为 未通过 的考试
     * @Date 2021/1/13 10:36
     * @Param [examManage]
     * @return boolean
     **/
    @Override
    public boolean modifyExam(ExamManage examManage) {
        return examManageMapper.updateByPrimaryKeySelective(examManage)>0;
    }

}
